package org.hbgl.geoentity.core.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 代码功能：10进制转62进制
 * 开发日期：2020/3/25
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
public class Ten2SixtyTwo {
    // 定义62进制数字
    private static final String X62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    // 拿到62进制转换10进制的值键对
    private static final HashMap<Character, Long> sixtyTwo2Ten = createMapSixtyTwo2Ten();
    // 拿到10进制转换62进制的值键对
    private static final HashMap<Long, Character> ten2SixtyTwo = createMapTen2SixtyTwo();
    // 定义静态进制数
    private static final long BASE = 62;

    private static HashMap<Character, Long> createMapSixtyTwo2Ten() {
        HashMap<Character, Long> map = new HashMap<>();
        for (int i = 0; i < X62.length(); i++) {
            // 0--0,... ..., z--61的对应存放进去
            map.put(X62.charAt(i), (long) i);
        }
        return map;
    }

    private static HashMap<Long, Character> createMapTen2SixtyTwo() {
        HashMap<Long, Character> map = new HashMap<>();
        for (int i = 0; i < X62.length(); i++) {
            // 0--0,... ..., 61--z的对应存放进去
            map.put((long) i, X62.charAt(i));
        }
        return map;
    }

    /**
     * 用递归来实现10 to 62
     *
     * @param iSrc
     * @return
     */
    public static String conversion(long iSrc) {
        String result = "";
        long key = iSrc / BASE;
        long value = iSrc % BASE;

        if (key != 0) {
            result += conversion(key);
        }

        result += ten2SixtyTwo.get(value).toString();
        return result;
    }

    /**
     * 用递归来实现10 to 62
     *
     * @param iSrc
     * @param length
     * @return
     */
    public static String conversion(long iSrc, int length) {
        String result = conversion(iSrc);
        String format = "%" + length + "s";
        result = String.format(format, result).replaceAll(" ", "0");
        return result;
    }

    public static List<String> conversionList(List<String> numberList) {
        List<String> result = new ArrayList<>();
        for (String number : numberList) {
            result.add(conversion(Long.parseLong(number), 10));
        }
        return result;
    }

    /**
     * 62 to 10
     *
     * @param pStr 62进制字符串
     * @return 十进制
     */
    public static long reversion(String pStr) {
        if (StringUtils.isBlank(pStr))
            return 0;
        // 目标十进制数初始化为0
        long deciaml = 0;
        // 记录次方,初始为62进制长度 -1
        int power = pStr.length() - 1;
        // 将62进制字符串转换成char[]
        char[] keys = pStr.toCharArray();
        for (int i = 0; i < pStr.length(); i++) {
            // 拿到62进制对应的10进制数
            long value = sixtyTwo2Ten.get(keys[i]);
            deciaml = deciaml + (long) (value * Math.pow(BASE, power));
            // 执行完毕 次方自减
            power--;
        }
        return deciaml;
    }

    public static void main(String[] args) {
        // zzzzzzzzzz = 83,9299,3658,6834,0223
        long l = reversion("zzzzzzzzzz");
        System.out.println(l);
        System.out.println(conversion(l, 10));
    }
}
